wx.cloud.init()
const db = wx.cloud.database()
const todosCollection = db.collection('data_user')


Page({
  data: {
    username: "",
    phonenumber: "",
    password: "",
    passwordack: "",
  },

  signin: function (e) {
    wx.redirectTo({ //这里进行页面跳转功能
      url: '/pages/login/login'
    })
  },
  regist: function (e) {
    var that = this
    console.log(that);
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (that.data.username == '') {
      wx.showModal({
        title: '提示！',
        content: '请输入用户名！',
        showCancel: false,
        success(res) {}
      })
    } else if (that.data.phonenumber == '') {
      wx.showModal({
        title: '提示！',
        content: '请输入手机号！',
        showCancel: false,
        success(res) {}
      })
    } else if (that.data.phonenumber.length != 11) {
      wx.showModal({
        title: '提示！',
        content: '手机号长度有误，请重新输入！',
        showCancel: false,
        success(res) {}
      })
    } else if (!myreg.test(that.data.phonenumber)) {
      wx.showModal({
        title: '提示！',
        content: '请输入正确的手机号码！',
        showCancel: false,
        success(res) {}
      })
    } else if (that.data.password == '') {
      wx.showModal({
        title: '提示！',
        content: '请输入密码！',
        showCancel: false,
        success(res) {}
      })
    } else if (that.data.passwordack == '') {
      wx.showModal({
        title: '提示！',
        content: '请输入确认密码！',
        showCancel: false,
        success(res) {}
      })
    } else if (that.data.password != that.data.passwordack) {
      wx.showModal({
        title: '提示！',
        content: '两次输入密码不一致！',
        showCancel: false,
        success(res) {}
      })
    } else {
      console.log("success");
      todosCollection.add({
        data:{
          password:that.data.password,
          phonenumber:that.data.phonenumber,
          username:that.data.username
        }
      }).then(res => {
        wx.showModal({
          content: '注册成功',
          showCancel: false,
          success(res) {}
        })
        console.log(res);
      })
    }  
  },
  usernameInput: function (e) {
    this.data.username = e.detail.value
  },
  phonenumberInput: function (e) {
    this.data.phonenumber = e.detail.value
  },
  passwordInput: function (e) {
    this.data.password = e.detail.value
  },
  passwordInputAck: function (e) {
    this.data.passwordack = e.detail.value
  },
  login_new:function(event) {
    wx.navigateTo({
      url: '../login/login.wxml',
    })
  },
 

})